package swle.xyz.austers.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import swle.xyz.austers.R;
import swle.xyz.austers.adapter.NewsAdapter;
import swle.xyz.austers.bean.News;

public class NewsActivity extends BaseActivity {
    private Toolbar toolbar;
    private String url = "http://news.aust.edu.cn/";
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    SwipeRefreshLayout swipeRefreshLayout;
    Handler handler;
    boolean isFirstInit = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        handler = new Handler();
        initView();
        initEvent();
        initDoc();
    }

    @Override
    public void initView() {
        toolbar = findViewById(R.id.toolbar_news_activity);
        setSupportActionBar(toolbar); //将toolbar设置为当前activity的操作栏
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//添加默认的返回图标
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });
        getSupportActionBar().setHomeButtonEnabled(true);//设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏toolbar默认显示的label

        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout_newsActivity);
        recyclerView = findViewById(R.id.recyclerView_NewsActivity);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void initEvent() {
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.aust));
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initDoc();
            }
        });
    }

    public void initDoc(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<News> newsList = new ArrayList<>();
                Document doc = null;
                try {
                    doc = Jsoup.connect("http://news.aust.edu.cn/xxxw.htm").get();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Elements content = doc.getElementsByClass("list-r-c");
                Elements ul = content.select("a");
                System.out.println(ul);
                System.out.println("--------------------------------------");
//        for (Element link: ul){
//            System.out.println(link.attr("href"));
//            System.out.println(link.attr("title"));
//
//        }
                for (int i=0;i<=14;i++){
                    Element link = ul.get(i);
                    News news = new News();
                    news.setTitle(link.attr("title"));
                    news.setUrl(link.attr("href"));
                    newsList.add(news);
                }
                final NewsAdapter newsAdapter = new NewsAdapter(newsList,getApplicationContext());
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setAdapter(newsAdapter);
                        if (isFirstInit){
                            swipeRefreshLayout.setRefreshing(false);
                        }else {
                            swipeRefreshLayout.setRefreshing(false);
                            Toast.makeText(NewsActivity.this,"刷新成功！",Toast.LENGTH_SHORT).show();
                        }
                        isFirstInit = false;

                    }
                };
                handler.post(runnable);

            }
        }).start();
    }


//
//    @Override
//    public void initEvent() {
//        webView.getSettings().setJavaScriptEnabled(true);
//        // 设置可以支持缩放
//        webView.getSettings().setSupportZoom(true);
//        // 设置出现缩放工具
//        webView.getSettings().setBuiltInZoomControls(true);
//        //扩大比例的缩放
//        webView.getSettings().setUseWideViewPort(true);
//        //自适应屏幕
//        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        webView.getSettings().setLoadWithOverviewMode(true);
//
////        webView.setWebChromeClient(new WebChromeClient(){});
//
//
//        webView.setWebViewClient(new WebViewClient() {
//
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                //该方法在Build.VERSION_CODES.LOLLIPOP以前有效，从Build.VERSION_CODES.LOLLIPOP起，建议使用shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead
//                //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
//                //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242
//
////                if (url.toString().contains("sina.cn")){
////                    view.loadUrl("http://ask.csdn.net/questions/178242");
////                    return true;
////                }
//
//                return false;
//            }
//
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
//            {
//                //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
//                //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242
////                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
////                    if (request.getUrl().toString().contains("sina.cn")){
////                        view.loadUrl("http://ask.csdn.net/questions/178242");
////                        return true;
////                    }
////                }
//
//                return false;
//            }
//
//        });
//        webView.loadUrl(url);
//    }
}
